<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>购买宝贝</title>
    <link rel="stylesheet" href="css/base.css" />
    <link rel="stylesheet" href="css/all.css" />
    <link rel="stylesheet" href="css/iconfont.css" />
    <link rel="stylesheet" href="css/buy-body.css" />
    <link rel="stylesheet" href="css/buy-body-address.css" />
</head>
<body>
<div>
    <div class="head">
         <span href="javascript:;" onclick="go()">
        <i class="iconfont icon-iconfontjiantou1"></i></span>
        <h1 class="titles">购买宝贝</h1>
    </div>
    <div class="middles">
        <div class="merchan row">
            <div class="merch">
                <img src="img/yifu.png" alt="">
            </div>
            <div class="introduce" id="orderProductInfo">
                <!--<p>夏日斜跨小包包女2018新款潮韩版百搭时尚</p>
                <span>￥15</span><span>快递：￥5</span>
                <div class="jian">
                    <i class="iconfont icon-jian"></i>
                </div>
                <div class="number">
                    <input type="text" placeholder="1" class="count"/>
                </div>
                <div class="jia">
                    <i class="iconfont icon-jiahao"></i>
                </div>-->
            </div>
        </div>
        <div id = "userAddress">
        </div>
        <div class="amount">
            <span>购买数量</span><span id="productNumber"></span>
        </div>
        <!--<div class="amount">
            <span>订单号：23256562354623</span>
        </div>-->
    </div>
    <div class="footer">
        <span class="pay">实付款：</span><span id="orderPrice"></span>
        <div onclick="submitOrder()" class="confirm">
            <span class="confirms">确定</span>
        </div>
    </div>
    <div>
        <input type = "hidden" id="submitProductId"/>
        <input type = "hidden" id="submitProductNumber"/>
        <input type = "hidden" id="submitUserAddress"/>
    </div>
</div>
<script src="js/x_rem.js"></script>
<script src="js/all.js"></script>
<script src="./js/jquery.min.js"></script>
<script>

    $(function() {
        var productId = GetQueryString("productId");
        var productNumber = GetQueryString("productNumber");
        var addressId = GetQueryString("addressId");
        //var productNumber = 1;
        //var productId = 28;
        var data = JSON.stringify({
            "productId": productId,
            "addressId":addressId
        });
        var success1 = (data) => {
            console.log(data.message.code);
            if(data.message.code == 200) {
                var html = "";
                if(data.data != null&& data.data != ''){
                    var nowPrice = data.data.nowPrice;
                    var freightPrice = data.data.freightPrice;
                    var productId = data.data.id;
                    var html =`
                        <p>${data.data.title}</p>
                        <span>商品价格￥:${data.data.nowPrice}</span><span>快递费￥:${data.data.freightPrice}</span>
                        <!--<div class="jian">
                        <i class="iconfont icon-jian"></i>
                        </div>-->
                        <div class="number">
                        <input type="text" placeholder="1" readonly="readonly" class="count"/>
                        </div>
                        <!--<div class="jia">
                        <i class="iconfont icon-jiahao"></i>
                        </div>-->
                    `;
                    $("#orderProductInfo").html(html);
                    $("#productNumber").html("总计" + productNumber + "件");
                    $("#orderPrice").html(productNumber * (nowPrice + freightPrice));
                    $("#submitProductId").val(productId);
                    $("#submitProductNumber").val(productNumber);
                }
            } else {
                popup(data.message.msg);
            }
        };
        Hajax("post", "/supplier/product/getProductDetail", data, success1);

        var success2 = (data) => {
            console.log(data.message.code);
            if(data.message.code == 200) {
                var html;
                if (data.data.defaultAddress != null && data.data.defaultAddress != '') {
                    var submitUserAddressId = data.data.defaultAddress.id;
                    html = `
                    <div onclick="location.href='./address-administration.html'" class="consignee">
                        <span>收货人：${data.data.defaultAddress.name}</span><span>${data.data.defaultAddress.mobile}</span>
                        <p>取货地址：${data.data.defaultAddress.pcadetail}${data.data.defaultAddress.address}</p>
                        <i class="iconfont icon-youjiantou1"></i>
                    </div>
                    `;
                    $("#userAddress").html(html);
                    $("#submitUserAddress").val(submitUserAddressId);
                } else {
                    html = `
                    <div onclick="location.href='./address-administration.html'" class="consign row">
                            <div class="add-r">
                            <span>添加地址</span>
                            </div>
                            <div class="consigns">
                            <img src="img/tianjia.png" alt="">
                            </div>
                            </div>
                    }
                    `;
                    $("#userAddress").html(html);
                }
            } else {
                popup(data.message.msg);
            }
        };
        Hajax("post", "/app/account/getDefaultAddress", data, success2);
    });

    function submitOrder() {
        $.ajax({
            type: "POST",
            url: "app/order/submitOrder",
            contentType: "application/json;charset=utf-8",
            data: JSON.stringify({
                "productId": $("#submitProductId").val(),
                "productNumber": $("#submitProductNumber").val(),
                "userAddressId": $("#submitUserAddress").val()
            }),
            dataType: "json",
            beforeSend: function (XMLHttpRequest) {
                XMLHttpRequest.setRequestHeader("token", token);
                XMLHttpRequest.setRequestHeader("userId", userId);
            },
            error: function (request) {
                alert("Connection error ");
            },
            success: function (data) {
                //layer.msg(data.message.msg);
                var orderId = data.data.orderId;
                var orderPrice = data.data.orderPrice;
                var u = navigator.userAgent;
                var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1 //android终端
                var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
//                if(isAndroid) {
                    location.href = 'orderpay://host/' + orderId + "," + orderPrice;
//                }
            }
        });
    }

</script>
</body>
</html>